package com.swxy.club.service.activity;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.swxy.club.dto.Activity;
import com.swxy.club.dto.ActivityReview;
import com.swxy.club.dto.bo.ActivityAddBo;
import com.swxy.club.dto.bo.ApproveActivityBo;
import com.swxy.club.dto.bo.CurrentUserBO;
import com.swxy.club.dto.bo.PageBo;
import com.swxy.club.enums.ActivityStatus;
import com.swxy.club.enums.AuditStatus;
import com.swxy.club.mapper.ActivityMapper;
import com.swxy.club.utils.UserContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * @author 易
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService{

    private final ActivityReviewService activityReviewService;

    @Override
    public Activity getActivityById(Integer id) {
        return this.getById(id);
    }

    @Override
    @Transactional
    public void addActivity(ActivityAddBo form) {

        // 获取当前登入的用户信息
        CurrentUserBO currentUser = UserContext.getCurrentUser();
        Assert.notNull(currentUser, "用户未登入");

        Activity activity = BeanUtil.copyProperties(form, Activity.class);

        // 传入的时间不能早于当前时间
        Assert.isTrue(activity.getStartTime().isAfter(LocalDateTime.now()) || activity.getStartTime().isEqual(LocalDateTime.now()), "活动开始时间不能早于当前时间");

        // 开始时间必须早于结束时间
        Assert.isTrue(activity.getStartTime().isBefore(activity.getEndTime()), "开始时间必须早于结束时间");
        // 两个时间间间隔不能低于1天
        Assert.isTrue(
                activity.getStartTime().plusDays(1).isBefore(activity.getEndTime()) ||
                        activity.getStartTime().plusDays(1).isEqual(activity.getEndTime()),
                "两个时间间隔不能低于1天");


        activity.setCurrentNumber(0);
        activity.setStatus(ActivityStatus.INVALID);
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        activity.setCreateId(currentUser.getId());
        activity.setUpdateId(currentUser.getId());

        this.save(activity);

        ActivityReview activityReview = new ActivityReview();
        activityReview.setActivityId(activity.getId());
        activityReview.setClubId(activity.getClubId());
        activityReview.setUserId(currentUser.getId());
        activityReview.setStatus(AuditStatus.PENDING);
        activityReview.setCreateTime(LocalDateTime.now());
        activityReview.setUpdateTime(LocalDateTime.now());
        activityReview.setCreateId(currentUser.getId());
        activityReview.setUpdateId(currentUser.getId());
        activityReviewService.save(activityReview);


    }

    @Override
    public Page<Activity> pageSelect(PageBo form) {

        LambdaQueryWrapper<Activity> queryWrapper = new QueryWrapper<Activity>().
                lambda()
                .like(form.getTitle() != null && !form.getTitle().isEmpty(), Activity::getTitle, form.getTitle())
                .eq(Activity::getStatus, ActivityStatus.VALID);
        return this.page(new Page<>(form.getPageNum(), form.getPageSize()), queryWrapper);
    }

    @Override
    public void approveActivity(ApproveActivityBo form) {
        ActivityStatus activityStatus = ActivityStatus.valueOf(form.getStatus());
        Assert.notNull(activityStatus, "状态错误");
        Activity activity = this.getById(form.getActivityId());
        Assert.notNull(activity, "活动不存在");

        ActivityReview reviewService = activityReviewService.getById(form.getActivityId());
        Assert.notNull(reviewService, "活动申请不存在");

        if (activityStatus == ActivityStatus.INVALID) {
            Assert.isFalse(form.getRefuseReason() == null || form.getRefuseReason().isEmpty(), "拒绝原因不能为空");
            reviewService.setRefuseReason(form.getRefuseReason());
            reviewService.setStatus(AuditStatus.REJECTED);
            reviewService.setOperateId(UserContext.getCurrentUser().getId());
            reviewService.setUpdateTime(LocalDateTime.now());
            activityReviewService.updateById(reviewService);

        }else{
            reviewService.setStatus(AuditStatus.APPROVED);
            reviewService.setOperateId(UserContext.getCurrentUser().getId());
            reviewService.setUpdateTime(LocalDateTime.now());

            activityReviewService.updateById(reviewService);

            activity.setStatus(ActivityStatus.VALID);
            activity.setUpdateTime(LocalDateTime.now());

            this.updateById(activity);
        }
    }
}
